package com.myzl.coal.service.impl;

import cn.hutool.core.util.BooleanUtil;
import com.myzl.coal.service.IAlarmEventService;
import com.myzl.coal.service.IDemoService;
import com.myzl.coal.service.IRfidService;
import com.myzl.common.enums.DemoEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName DemoService
 * @Author wzb
 * @Date 2023/6/15 13:58
 * @Description
 */
@Service
@Data
@Slf4j
public class DemoService implements IDemoService {

    private static final String switchDemoKey = "switchDemo";

    private final StringRedisTemplate redisTemplate;
    private final IRfidService rfidService;
    private final IAlarmEventService alarmEventService;


    @Override
    public boolean getSwitch(String flag) {
        Map<String, Boolean> allSwitch = getAllSwitch();
        return allSwitch.get(flag);
    }

    @Override
    public Map<String, Boolean> getAllSwitch() {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(switchDemoKey);
        Map<String, Boolean> booleanMap = entries.entrySet().stream().collect(Collectors.toMap(e -> String.valueOf(e.getKey()), e -> toBoolean(e.getValue())));
        return booleanMap;
    }
    @Override
    public void setRedisPoliceFlag(DemoEnum demoEnum, Boolean flag) {
        redisTemplate.opsForHash().put(switchDemoKey, demoEnum.getCode(),flag.toString());
    }

    @Override
    public boolean getRedisPoliceFlag(DemoEnum demoEnum) {
        Object o = redisTemplate.opsForHash().get(switchDemoKey, demoEnum.getCode());
        return toBoolean(o);
    }


    private boolean toBoolean(Object o) {
        boolean b = o == null ? false : BooleanUtil.toBoolean(o.toString());
        return b;
    }
}
